import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutterhello/api/api.dart';
import 'package:flutterhello/controller/Controller.dart';
import 'package:flutterhello/pages/one/widgets/activiteCard.dart';
import 'package:get/get.dart';

class OnePage extends StatefulWidget {
  @override
  _OnePageState createState() => _OnePageState();
}

class _OnePageState extends State<OnePage> with AutomaticKeepAliveClientMixin {
  @override
  bool get wantKeepAlive => true;

  var page = 1;
  var pageSize = 20;
  var list = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  void refresh() async {
    this.page = 1;
    this.pageSize = 20;
    var res = await Api.getActivites({"page": page, "pageSize": pageSize});
    this.list = res['data']['activite_list'];
    if (mounted) {
      setState(() {});
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: PreferredSize(
        child: AppBar(
          title: Text('活动'),
          centerTitle: true,
          actions: <Widget>[
            IconButton(
              icon: Icon(
                Icons.add,
                color: Colors.white,
              ),
              onPressed: () {
                Get.toNamed('/add_act'); // do something
              },
            ),
          ],
        ),
        preferredSize: Size.fromHeight(50),
      ),
      body: EasyRefresh.custom(
        firstRefresh: true,
        firstRefreshWidget: Container(
          width: double.infinity,
          height: double.infinity,
          color: Colors.white,
          child: Center(
            child: CircularProgressIndicator(),
          ),
        ),
        header: MaterialHeader(),
        footer: MaterialFooter(),
        onRefresh: () async {
          await this.refresh();
        },
        slivers: <Widget>[
          SliverToBoxAdapter(
            child: Padding(
              padding:
                  EdgeInsets.only(left: 20, right: 20, top: 10, bottom: 10),
              child: Container(
                width: double.infinity,
                height: 160,
                child: ClipRRect(
                  borderRadius: BorderRadius.all(Radius.circular(10)),
                  child: Image.network(
                    'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201808%2F15%2F20180815093329_ilxkw.jpg&refer=http%3A%2F%2Fb-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1623327459&t=c8f35d37bb7d608c481ae700a41d143a',
                    fit: BoxFit.cover,
                  ),
                ),
              ),
            ),
          ),
          SliverList(
            delegate: SliverChildBuilderDelegate(
              (BuildContext context, int index) {
                return ActiviteCard(
                  info: this.list[list.length - index - 1],
                );
              },
              childCount: list.length,
            ),
          )
        ],
      ),
    );
  }
}
